/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.InsumoException;
import Negocio.Empresa;
import Negocio.Proveedor;
import Negocio.TipoDeInsumo;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class FrmTipoInsumo extends javax.swing.JDialog {
    private Empresa unaEmpresa;
    private TipoDeInsumo unTipoDeInsumo;
    private DefaultListModel modelTiposDeInsumos = new DefaultListModel();

  
    /**
     * Creates new form FrmTipoInsumo
     */
    public FrmTipoInsumo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public FrmTipoInsumo(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmpresa = unaEmpresa;
        this.inicializar();
    }
    public void inicializar(){
        this.LstTiposDeInsumo.setModel(modelTiposDeInsumos);
        this.cargarTiposDeInsumos();
        this.limpiarTexts();
        this.deshablitar();
        this.unTipoDeInsumo = null;
        
    }
    public void hablitar(){
        this.TxtNombre.setEnabled(true);
        this.TxtDescripcion.setEnabled(true);
        this.TxtUnidad.setEnabled(true);

        this.jBtnAgregar.setEnabled(false);
        this.BntGuardar.setEnabled(true);
        this.BtnBorrar.setEnabled(true);
        this.BtnCacel.setEnabled(true);
    }
    
     public void deshablitar(){
        this.TxtNombre.setEnabled(false);
        this.TxtDescripcion.setEnabled(false);
        this.TxtUnidad.setEnabled(false);

        this.jBtnAgregar.setEnabled(true);
        this.BntGuardar.setEnabled(false);
        this.BtnBorrar.setEnabled(false);
        this.BtnCacel.setEnabled(false);
        
    }
    public void limpiarTexts(){
            this.TxtDescripcion.setText("");
            this.TxtNombre.setText("");
            this.TxtUnidad.setText("");
            
            
    }
   public void cargarTiposDeInsumos(){
        this.modelTiposDeInsumos.clear();             
        if(this.unaEmpresa.getTiposDeInsumosActivos().size() > 0){
            this.unaEmpresa.ordenarTipoDeInsumo(this.unaEmpresa.getTiposDeInsumos(),1);
            Iterator it = this.unaEmpresa.getTiposDeInsumosActivos().iterator();
            while (it.hasNext()) {
                this.modelTiposDeInsumos.addElement((TipoDeInsumo) it.next());
            }
        }else{
            this.modelTiposDeInsumos.addElement("No se Encontraron Tipos de Insumo");
        }        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        LstTiposDeInsumo = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        TxtNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        TxtDescripcion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        TxtUnidad = new javax.swing.JTextField();
        jBtnAgregar = new javax.swing.JButton();
        BntGuardar = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();
        BtnAceptar = new javax.swing.JButton();
        BtnCacel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Tipos De Insumos");

        LstTiposDeInsumo.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstTiposDeInsumo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstTiposDeInsumoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LstTiposDeInsumo);

        jLabel2.setText("Nombre");

        TxtNombre.setEnabled(false);

        jLabel3.setText("Descripcion");

        TxtDescripcion.setEnabled(false);

        jLabel4.setText("Unidad");

        TxtUnidad.setEnabled(false);

        jBtnAgregar.setText("Nuevo");
        jBtnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAgregarActionPerformed(evt);
            }
        });

        BntGuardar.setText("Guardar");
        BntGuardar.setEnabled(false);
        BntGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BntGuardarActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.setEnabled(false);
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BtnAceptarMouseClicked(evt);
            }
        });

        BtnCacel.setText("Cancelar");
        BtnCacel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCacelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(TxtNombre)
                                    .addComponent(TxtDescripcion)
                                    .addComponent(TxtUnidad)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnAgregar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BntGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnBorrar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnCacel)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(BtnAceptar))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(TxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(TxtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(TxtUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBtnAgregar)
                            .addComponent(BntGuardar)
                            .addComponent(BtnBorrar)
                            .addComponent(BtnCacel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 296, Short.MAX_VALUE)
                        .addComponent(BtnAceptar)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LstTiposDeInsumoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstTiposDeInsumoMouseClicked
        // TODO add your handling code here:
            this.unTipoDeInsumo = (TipoDeInsumo)this.modelTiposDeInsumos.getElementAt(this.LstTiposDeInsumo.getSelectedIndex());      
            this.TxtDescripcion.setText(this.unTipoDeInsumo.getDescripcion());
            this.TxtNombre.setText(this.unTipoDeInsumo.getNombre());
            this.TxtUnidad.setText(this.unTipoDeInsumo.getUnidad());
            this.hablitar();
            
            

    }//GEN-LAST:event_LstTiposDeInsumoMouseClicked

    private void jBtnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAgregarActionPerformed
        // TODO add your handling code here:       
        this.limpiarTexts();
        this.hablitar();
        this.unTipoDeInsumo = null;
    }//GEN-LAST:event_jBtnAgregarActionPerformed

    private void BntGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BntGuardarActionPerformed
               // TODO add your handling code here:
        
        try{
            if(this.unTipoDeInsumo != null){
                if(!this.unaEmpresa.ExisteTipoDeInsumo(this.unTipoDeInsumo,this.TxtNombre.getText())){                    
                    this.unTipoDeInsumo.actualizarDatos(this.TxtNombre.getText(), this.TxtDescripcion.getText(),this.TxtUnidad.getText());
                }
            }else{            
                this.unaEmpresa.nuevoTipoDeInsumo(this.TxtNombre.getText(), this.TxtDescripcion.getText(),this.TxtUnidad.getText());
            }
            JOptionPane.showMessageDialog(this, "La operacion se realizo correctamente");
            this.limpiarTexts();
            this.deshablitar();
            this.cargarTiposDeInsumos();
        }
        catch(InsumoException ex){
             JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_BntGuardarActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
               // TODO add your handling code here:
        int mc,bc,ch=1;
        mc = JOptionPane.QUESTION_MESSAGE;
        bc = JOptionPane.YES_NO_CANCEL_OPTION;
        ch = JOptionPane.showConfirmDialog (this, "Desea dar de baja al tipo de Insumo?", "Confirmacion", bc, mc); 
        if(ch == 0){  
            this.unTipoDeInsumo.deshabilitar();
        }
        this.limpiarTexts();
        this.deshablitar();
        this.cargarTiposDeInsumos();
    }//GEN-LAST:event_BtnBorrarActionPerformed

    private void BtnAceptarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BtnAceptarMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnAceptarMouseClicked

    private void BtnCacelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCacelActionPerformed
        // TODO add your handling code here:
        this.limpiarTexts();
        this.deshablitar();
        this.cargarTiposDeInsumos();
    }//GEN-LAST:event_BtnCacelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmTipoInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmTipoInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmTipoInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmTipoInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmTipoInsumo dialog = new FrmTipoInsumo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BntGuardar;
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnCacel;
    private javax.swing.JList LstTiposDeInsumo;
    private javax.swing.JTextField TxtDescripcion;
    private javax.swing.JTextField TxtNombre;
    private javax.swing.JTextField TxtUnidad;
    private javax.swing.JButton jBtnAgregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
